﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DataTransfer;
namespace Gui
{
    public partial class frmLapPhieuKhamBenh : DevComponents.DotNetBar.Office2007Form
    {
        public frmLapPhieuKhamBenh(DSKB info)
        {
            InitializeComponent();
            if (info != null)
            {
                txtHoTen.Text = info.HoTen;
                txtMaBenhNhan.Text = info.ID.ToString();
                dtpNgayKham.Value =Convert.ToDateTime(info.NgayKham);
                lbMaPhieuKham.Visible = false;
                txtMaPhieuKham.Visible = false;
            }
        }

        private void btnThemThuoc_Click(object sender, EventArgs e)
        {
            Form frm = new frmPhieuKhamChiTiet(this);
            frm.ShowDialog();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
           
            try
            {
                int idpk = InsertPhieuKham();
                int idbn=InsertBenhNhan(idpk);
                MessageBox.Show("Save Success");
            }
            catch (Exception ex)
            {
                MessageBox.Show("Save Error:" + ex.Message);
                //throw;
            }
        }
        private int InsertBenhNhan(int idPhieukham)
        {
            int idDsKb = Convert.ToInt32(txtMaBenhNhan.Text);
            DSKBBus dskbBus = new DSKBBus();
            DSKB bn = dskbBus.Get(idDsKb);
            BenhNhan info = new BenhNhan();
            info.HoTen = bn.HoTen;
            info.GioiTinh = bn.GioiTinh;
            info.DiaChi = bn.DiaChi;
            info.NamSinh = bn.NamSinh;
            info.IDPhieuKham = idPhieukham;
            info.NgayKham = dtpNgayKham.Value.Date;
            info.UserCreate = frmMain.currentUser.UserName;
            info.CreateDate = DateTime.Now;
            BenhNhanBus bnBus = new BenhNhanBus();
            int id= bnBus.Insert(info);
            return id;
        }
        private int InsertPhieuKham()
        {
            PhieuKham info = new PhieuKham();
            LoaiBenhBus lbenhBus = new LoaiBenhBus();
            info.LoaiBenh = lbenhBus.Get(Convert.ToInt32(cbxLoaiBenh.SelectedValue.ToString())).TenLoaiBenh;
            info.TrieuChung = txtTrieuChung.Text;
            info.UserCreate = frmMain.currentUser.UserName;
            info.CreateDate = DateTime.Now;
            PhieuKhamBus pkBus = new PhieuKhamBus();
            int id = pkBus.Insert(info);
            if (dtgThuoc.Rows.Count > 0)
            {
                foreach (DataGridViewRow row in dtgThuoc.Rows)
                {

                    if (row.IsNewRow) continue;
                        PhieuKhamChiTiet data = new PhieuKhamChiTiet();
                        data.IDPhieuKham = id;
                        data.IDTHuoc = Convert.ToInt32(row.Cells[0].Value.ToString());
                        data.DonVi = row.Cells[2].Value.ToString();
                        data.SoLuong = Convert.ToInt32(row.Cells[3].Value.ToString());
                        data.CachDung = row.Cells[4].Value.ToString();
                        data.CreateDate = DateTime.Now;
                        data.UserCreate = frmMain.currentUser.UserName;
                        int idpkct = InsertPhieuKhamChiTiet(data);

                    
                }
            }
           return id;
         
        }
        private int InsertPhieuKhamChiTiet(PhieuKhamChiTiet info)
        {
            PhieuKhamChiTietBus pkctBus = new PhieuKhamChiTietBus();
            int id = pkctBus.Insert(info);
                return id;

        }

        private void frmLapPhieuKhamBenh_Load(object sender, EventArgs e)
        {
            addColumn();
            LoaiBenhBus lbBus = new LoaiBenhBus();
            cbxLoaiBenh.DataSource = lbBus.GetAll();
            cbxLoaiBenh.DisplayMember = "TenLoaiBenh";
            cbxLoaiBenh.ValueMember = "ID";

        }
        public void addColumn()
        {

            dtgThuoc.ColumnCount = 5;
            dtgThuoc.Columns[0].Name = "IDThuoc";
           // dtgThuoc.Columns[0].Visible = false;
            dtgThuoc.Columns[1].Name = "Thuốc";
            dtgThuoc.Columns[2].Name = "Đơn Vị";
            dtgThuoc.Columns[3].Name = "Số Lượng";
            dtgThuoc.Columns[4].Name = "Cách Dùng";

        }
        public void addrow(string[] row)
        {
            dtgThuoc.Rows.Add(row);
        }
        public void refresh()
        {
            dtgThuoc.Refresh();
        }

        private void btnXoaThuoc_Click(object sender, EventArgs e)
        {
            int index = dtgThuoc.CurrentRow.Index;
            if (index >= 0)
            {
                dtgThuoc.Rows.RemoveAt(index);
            }
            else
            {
                MessageBox.Show("Chua Chon Item");
            }
        }

    }
}
